<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Http\Controllers\admin\GoodsController;
class CarouselController extends Controller
{
    /**
    *获取商品分类
    */
    public function getCate($id)
    {
        $data = DB :: table('shop_cate') -> where('pid', $id) -> select('id', 'cname') -> get();
        return $data;
    }
    /**
    *获取大类下的小类
    */
    public function getCate2(Request $request)
    {
        $id = $request ->input('id');
        $data = self::getCate($id);
        if($data)
            echo json_encode($data);
    }
    /**
    *
    *轮播图添加页面
    */
    public function getAdd()
    {
        $data = self::getCate(0);
        return view('/admin/carousel/add', ['data' => $data]);
    }
    /**
    *执行轮播图添加
    */
    public function postInsert(Request $request)
    {
        $arr = $request -> except(['_token', 'pid']);
        if($request -> hasFile('cpic'))
            $arr = GoodsController::uploadPic('cpic', $request, $arr);
        $res = DB :: table('shop_carousel') ->insert($arr);
        if($res)
            return redirect('/admin/carousel/index') ->with('success', '轮播图添加成功');
        else
            return back() -> withInput() -> with('error', '轮播图添加失败');          
    }
    /**
    *轮播图展示
    */
    public function getIndex(Request $request)
    {
        $count = $request -> input('count',10);
        $search = $request -> input('search','');
        //带条件查询数据，并且分页
        $data = DB::table('shop_carousel') ->join('shop_goods', 'shop_goods.id', '=', 'shop_carousel.gid') ->select('shop_carousel.id as id','ctype','gid','goodsdes','cpic') ->where('goodsdes','like','%'.$search.'%') ->orderBy('id', 'asc')->paginate($count);
        $page = $request -> input('page',1);
        $start = ($page - 1) * $count + 1;
        $sum = count(DB::table('shop_carousel') ->join('shop_goods', 'shop_goods.id', '=', 'shop_carousel.gid') ->where('goodsdes','like','%'.$search.'%')->get()); 
        $end = min($page * $count,$sum);
        return view('/admin/carousel/index',['data'=>$data,'request'=>$request -> all(), 'start' => $start, 'end' => $end, 'sum' => $sum]);
    }
    /**
    *轮播图修改页面
    *
    *@param $id int 接受要修改的轮播图ID
    */
    public function getEdit($id)
    {
        $data = self::getCate(0);
        $data1 = DB :: table('shop_carousel') ->join('shop_goods', 'shop_goods.id', '=', 'shop_carousel.gid') ->join('shop_cate', 'shop_cate.id', '=', 'shop_goods.tid') ->select('shop_carousel.id as id', 'ctype', 'cpic','articlenum', 'pid', 'cname') ->where('shop_carousel.id', $id) ->first();
        //dd($data1);
        return view('/admin/carousel/edit', ['data' => $data, 'data1' => $data1]);
    }

    /**
    *执行轮播图修改
    */
    public function postUpdate(Request $request)
    {
        $arr = $request -> except(['_token', 'id', 'pid']);
        $id = $request ->input('id');
        //判断是否有照片修改
        if($request -> hasFile('cpic'))
            $arr = GoodsController::uploadPic('cpic', $request, $arr);
        $res = DB :: table('shop_carousel') ->where('id', $id) ->update($arr);
        if($res)
            return redirect('/admin/carousel/index') ->with('success', '轮播图修改成功');
        else
            return back() -> withInput() -> with('error', '轮播图修改失败');
    }
    /**
    *执行轮播图删除
    */
    public function getDelete($id)
    {
        $res = DB :: table('shop_carousel') ->where('id', $id) ->delete();
        if($res)
            return redirect('/admin/carousel/index') ->with('success', '轮播图删除成功');
        else
            return back() -> withInput() -> with('error', '轮播图删除失败');
    }
}
